package com.secondGroup.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.secondGroup.config.OperationNum;
import com.secondGroup.entity.ElectronicAccount;
import com.secondGroup.entity.Flow;
import com.secondGroup.entity.User;
import com.secondGroup.mapper.FlowMapper;
import com.secondGroup.service.ElectronicAccountService;
import com.secondGroup.service.FlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.secondGroup.utils.GetTimeUtil;
import com.secondGroup.vo.StringVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Service
public class FlowServiceImpl extends ServiceImpl<FlowMapper, Flow> implements FlowService {

    @Autowired
    FlowMapper flowMapper;
    @Autowired
    ElectronicAccountService electronicAccountService;
    @Override
    @Transactional
    public StringVo transferAccount(User userInfo, BigDecimal amount, String toEid, String payPassword) {
        //获取用户电子账户信息
        StringVo stringVo = new StringVo();
        ElectronicAccount electronicAccount = electronicAccountService.getOne(new QueryWrapper<ElectronicAccount>()
                .lambda()
                .eq(ElectronicAccount::getEId, userInfo.getEId())
                .select(ElectronicAccount::getEName,ElectronicAccount::getEMoney,ElectronicAccount::getEPayPassword));
        if(payPassword!=null&&payPassword.equals(electronicAccount.getEPayPassword())){
            if(electronicAccount.getEMoney().compareTo(amount)<0){
                stringVo.setI(3);
                return stringVo;
            }
            BigDecimal fromMoney = electronicAccount.getEMoney().subtract(amount);
            electronicAccountService.update(new UpdateWrapper<ElectronicAccount>()
                    .lambda()
                    .eq(ElectronicAccount::getEId,userInfo.getEId())
                    .set(ElectronicAccount::getEMoney,fromMoney));
            //获取对方用户电子账户信息
            ElectronicAccount toElectronicAccount = electronicAccountService.getOne(new QueryWrapper<ElectronicAccount>()
                    .lambda()
                    .eq(ElectronicAccount::getEId, toEid)
                    .select(ElectronicAccount::getEMoney,ElectronicAccount::getEName)
            );
            //给对方电子账户加钱
            BigDecimal toMoney = toElectronicAccount.getEMoney().add(amount);
            electronicAccountService.update(new UpdateWrapper<ElectronicAccount>()
                    .lambda()
                    .eq(ElectronicAccount::getEId,toEid)
                    .set(ElectronicAccount::getEMoney,toMoney));
            //插入传出流水记录
            Flow flow = new Flow();
            flow.setFId(IdWorker.getIdStr());
            flow.setAccount(userInfo.getAccount());
            flow.setBalance(fromMoney);
            flow.setTradeMoney(amount);
            flow.setTradeTime(LocalDateTime.now());
            flow.setTradeType(OperationNum.ZhuanChu);
            flow.setTId(toEid);
            flow.setTName(toElectronicAccount.getEName());
            flow.setEId(userInfo.getEId());
            flow.setIId(userInfo.getEId());
            flow.setIName(electronicAccount.getEName());
            flowMapper.insert(flow);
            stringVo.setI(1);
            stringVo.setS(toElectronicAccount.getEName());
            return stringVo;
        }stringVo.setI(2);
        return stringVo;

    }


    /**
     * 用户获取默认流水
     * @param user
     * @return
     */
    @Override
    public Page<Flow> getDefaultFlow(User user,String pageNum) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beforeOneMonthDay = GetTimeUtil.getBeforeOneMonthDay(now);
        if(pageNum==null){
            Page<Flow> page = page(new Page<Flow>(1, 10), new QueryWrapper<Flow>()
                    .lambda()
                    .eq(Flow::getIId, user.getEId())
                    .or().eq(Flow::getTId,user.getEId())
                    .between(Flow::getTradeTime,beforeOneMonthDay,now)
                    .orderByAsc(Flow::getTradeTime));
            return page;
        }
        long currentPage = Long.parseLong(pageNum);
        Page<Flow> page = page(new Page<Flow>(currentPage, 10), new QueryWrapper<Flow>()
                .lambda()
                .eq(Flow::getIId, user.getEId())
                .or().eq(Flow::getTId,user.getEId())
                .between(Flow::getTradeTime,beforeOneMonthDay,now)
                .orderByAsc(Flow::getTradeTime));
        return page;

    }

    /**
     * 通过条件获取流水
     * @param user
     * @param tradeTime
     * @param tradeType
     * @return
     */
    @Override
    public List<Flow> getFlowByT(User user, int[] tradeTime, int tradeType) {

        LocalDateTime begin = LocalDateTime.of(tradeTime[0], tradeTime[1], tradeTime[2], 0, 0);
        LocalDateTime end=LocalDateTime.of(tradeTime[0], tradeTime[1], tradeTime[2],23,59,59);
        if(tradeType==5){
            List<Flow> list = list(new QueryWrapper<Flow>()
                    .lambda()
                    .eq(Flow::getIId, user.getEId())
                    .or()
                    .eq(Flow::getTId, user.getEId())
                    .between(Flow::getTradeTime, begin, end)
                    .orderByAsc(Flow::getTradeTime));
            return list;
        }
        List<Flow> list = list(new QueryWrapper<Flow>()
                .lambda()
                .eq(Flow::getIId, user.getEId())
                .or()
                .eq(Flow::getTId, user.getEId())
                .between(Flow::getTradeTime, begin, end)
                .eq(Flow::getTradeType, tradeType)
                .orderByAsc(Flow::getTradeTime));
        return list;
    }
}
